package org.zjx.app.action.association;


import org.zjx.analysis.analyze.association.AssociationHomeAnalysis;
import org.zjx.analysis.analyze.association.AssociationPurchaseAnalysis;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.app.service.AssociationService;
import org.zjx.command.entity.Association;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

public class AssociationHomeAction extends AbstractWindowAction {
    private AssociationHomeAnalysis associationHomeAnalysis;
    private AssociationPurchaseAnalysis associationPurchaseAnalysis;
    private final String itemName;

    public AssociationHomeAction(IWindow targetWindow, String itemName) {
        super(targetWindow);
        this.itemName = itemName;
    }

    @Override
    public void open() {
    }

    @Override
    public void close() {
        analysisHome();
        if (!associationHomeAnalysis.isOpened()) return;

        Rectangle closeButton = associationHomeAnalysis.getCloseButtonPoint();
        targetWindow.actions().clickIn(closeButton).delay(300);
        targetWindow.flushScreenshot();
        close();
    }

    private void analysisHome() {
        associationHomeAnalysis = new AssociationHomeAnalysis(targetWindow);
    }

    private void analysisPurchase() {
        associationPurchaseAnalysis = new AssociationPurchaseAnalysis(targetWindow);
    }

    @Override
    public void reSetWindow() {
        if (associationHomeAnalysis.isTransborder()) {
            Rectangle from = associationHomeAnalysis.conversion(associationHomeAnalysis.windowArea, associationHomeAnalysis.getRefImageArea());
            Rectangle to = associationHomeAnalysis.conversion(associationHomeAnalysis.getOriginBounds(), associationHomeAnalysis.getRefImageArea());
            targetWindow.drag(from.x,from.y,to.x,to.y);
            analysisHome();
        }
    }

    public void mouseAloofWindow(Rectangle rect) {
        if (associationHomeAnalysis.mouseInRect(rect)) {
            Point safePoint = associationHomeAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.moveTo(safePoint.x, safePoint.y);
        }
    }

    @Override
    protected void doExecute() throws Exception {
        analysisHome();
        if (!associationHomeAnalysis.isOpened()) return;
        reSetWindow();

        List<Association> associationList = AssociationService.getAssociations(itemName);

        for (Association association : associationList) {
            jumpToChamber(association.getShopId());
            AssociationPurchaseAction associationPurchaseAction = new AssociationPurchaseAction(targetWindow, association);
            associationPurchaseAction.execute();
            if(associationPurchaseAction.isOkBuy()) break;
        }
        close();
    }

    public void jumpToChamber(int chamberNumber) {
        analysisPurchase();
        while (!associationPurchaseAnalysis.isOpened()) {

            while (associationHomeAnalysis.getChamberNumber() != 0 && associationHomeAnalysis.getChamberNumber() != chamberNumber) {
                targetWindow.actions().hotkey(KeyEvent.VK_BACK_SPACE).delay(300);
                targetWindow.flushScreenshot();
                analysisHome();
            }
            if ( associationHomeAnalysis.getChamberNumber() != chamberNumber){
                targetWindow.input(chamberNumber + "");
            }
            targetWindow.actions().clickIn(associationHomeAnalysis.getJumpRectangle()).delay(1000);
            targetWindow.flushScreenshot();
            analysisPurchase();
        }
    }

}
